package com.example.userpage;

import java.io.File;
import java.io.FileInputStream;
import java.sql.*;

public class InsertImage {

    // ==== ✅ 1. 修改这些参数为你自己的配置 ====
    private static final String DB_URL = "jdbc:mysql://localhost:3306/message?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "//Sjj20051206";

    // 用户名 和 图片路径
    private static final String TARGET_USERNAME = "SJJ";
    private static final String IMAGE_PATH = "D:/Private/PracticalTraining/Front-End/src/Images/keine.png";
    // =============================================

    public static void main(String[] args) {
        uploadUserImage(TARGET_USERNAME, IMAGE_PATH);
    }

    public static void uploadUserImage(String username, String imagePath) {
        String sql = "UPDATE user SET image = ? WHERE username = ?";

        try (
                Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                PreparedStatement stmt = conn.prepareStatement(sql);
                FileInputStream fis = new FileInputStream(new File(imagePath))
        ) {
            stmt.setBinaryStream(1, fis, fis.available());
            stmt.setString(2, username);

            int rows = stmt.executeUpdate();
            if (rows > 0) {
                System.out.println("✅ 图片成功插入给用户：" + username);
            } else {
                System.out.println("⚠️ 未找到用户名为 " + username + " 的用户记录");
            }

        } catch (Exception e) {
            System.err.println("❌ 插入图片失败：");
            e.printStackTrace();
        }
    }
}
